/*
 * MIT License
 *
 * Copyright (c) 2024 org.zzz (https://gitee.com/frostforest)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.zzz.pcdc;

public class PcdcThreadConfig {

    public static final int DEFAULT_POLLING_INTERVAL_SECONDS = 10;
    public static final int DEFAULT_THROTTLE_SECONDS = 10;
    public static final int DEFAULT_BATCH_WORK_SIZE = 1000;
    public static final int DEFAULT_BATCH_WORK_TIMEOUT_SECONDS = 300;
    public static final int DEFAULT_PARTITION_THRESHOLD = 5_0000;
    public static final int DEFAULT_MIN_PARTITION_INTERVAL_SECONDS = 30;
    public static final int DEFAULT_WORKER_POOL_SIZE = 2;

    public static final int MIN_POLLING_INTERVAL_SECONDS = 1;
    public static final int MIN_THROTTLE_SECONDS = 1;
    public static final int MAX_THROTTLE_SECONDS = 3600;
    public static final int MIN_BATCH_WORK_SIZE = 10;
    public static final int MIN_BATCH_WORK_TIMEOUT_SECONDS = 30;
    public static final int FORCE_SAVE_PROGRESS_SECONDS = 15;

    public static final int MIN_PARTITION_THRESHOLD = 1_0000;
    public static final int MIN_MIN_PARTITION_INTERVAL_SECONDS = 10;
    public static final int MIN_WORKER_POOL_SIZE = 1;

    private final int workerPoolSize;
    private final int pollingIntervalSeconds;
    private final int throttleSeconds;
    private final int batchWorkSize;
    private final int batchWorkTimeoutSeconds;
    private final int partitionThreshold;
    private final int minPartitionIntervalSeconds;

    public PcdcThreadConfig() {
        this(DEFAULT_POLLING_INTERVAL_SECONDS,
                DEFAULT_THROTTLE_SECONDS,
                DEFAULT_BATCH_WORK_SIZE,
                DEFAULT_BATCH_WORK_TIMEOUT_SECONDS,
                DEFAULT_PARTITION_THRESHOLD,
                DEFAULT_MIN_PARTITION_INTERVAL_SECONDS,
                DEFAULT_WORKER_POOL_SIZE);
    }


    public PcdcThreadConfig(int pollingIntervalSeconds,
                            int throttleSeconds,
                            int batchWorkSize,
                            int batchWorkTimeoutSeconds,
                            int partitionThreshold,
                            int minPartitionIntervalSeconds,
                            int workerPoolSize) {
        this.pollingIntervalSeconds = Math.max(MIN_POLLING_INTERVAL_SECONDS, pollingIntervalSeconds);
        this.throttleSeconds = Math.min(Math.max(MIN_THROTTLE_SECONDS, throttleSeconds), MAX_THROTTLE_SECONDS);
        this.batchWorkSize = Math.max(MIN_BATCH_WORK_SIZE, batchWorkSize);
        this.batchWorkTimeoutSeconds = Math.max(MIN_BATCH_WORK_TIMEOUT_SECONDS, batchWorkTimeoutSeconds);
        this.partitionThreshold = Math.max(MIN_PARTITION_THRESHOLD, partitionThreshold);
        this.minPartitionIntervalSeconds = Math.max(MIN_MIN_PARTITION_INTERVAL_SECONDS, minPartitionIntervalSeconds);
        this.workerPoolSize = Math.max(MIN_WORKER_POOL_SIZE, workerPoolSize);
    }

    public int getPollingIntervalSeconds() {
        return pollingIntervalSeconds;
    }

    public int getThrottleSeconds() {
        return throttleSeconds;
    }

    public int getBatchWorkSize() {
        return batchWorkSize;
    }

    public int getBatchWorkTimeoutSeconds() {
        return batchWorkTimeoutSeconds;
    }

    public int getWorkerPoolSize() {
        return workerPoolSize;
    }

    public int getPartitionThreshold() {
        return partitionThreshold;
    }

    public int getMinPartitionIntervalSeconds() {
        return minPartitionIntervalSeconds;
    }
}
